package com.naodong.shenluntiku.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerFeedbackComponent;
import com.naodong.shenluntiku.di.module.FeedbackModule;
import com.naodong.shenluntiku.mvp.contract.FeedbackContract;
import com.naodong.shenluntiku.mvp.presenter.FeedbackPresenter;
import com.naodong.shenluntiku.mvp.view.widget.imagepickeview.ImageAddPickerListener;
import com.naodong.shenluntiku.mvp.view.widget.imagepickeview.ImageAddPickerView;
import com.naodong.shenluntiku.util.ImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.util.ToastUtil;
import me.shingohu.man.util.ViewUtil;

/**
 * Created by hujie on 17/4/25.
 */


public class FeedbackActivity extends BaseMVPActivity<FeedbackPresenter> implements FeedbackContract.View {

    @BindView(R.id.contentET)
    EditText contentET;
    @BindView(R.id.contactET)
    EditText contactET;

    @BindView(R.id.menuView)
    LinearLayout menuView;
    TextView submitTV;

    @BindView(R.id.imageAddpickerView)
    ImageAddPickerView imageAddPickerView;


    @Override
    protected void onCreateInit(Bundle savedInstanceState) {
        addSubmitBtn();
        initAddpickerView();
    }

    private void initAddpickerView() {
        imageAddPickerView.setImageAddPickerListener(new ImageAddPickerListener() {
            @Override
            public void onLoadImage(String path, ImageView imageView) {
                ImageLoaderUtil.loadImage(mActivity, path, imageView);
            }

            @Override
            public void onClickAdd(int remainNum) {

                List<LocalMedia> localMedias = new ArrayList<LocalMedia>();
                if (imageAddPickerView.getSelectList() != null && imageAddPickerView.getSelectList().size() > 0) {
                    for (String s : imageAddPickerView.getSelectList()) {
                        LocalMedia localMedia = new LocalMedia();
                        localMedia.setPath(s);
                        localMedias.add(localMedia);
                    }
                }
                PictureSelector.create(mActivity)
                        .openGallery(PictureMimeType.ofImage())
                        .selectionMode(PictureConfig.MULTIPLE)
                        .isCamera(false)
                        .isGif(false)
                        .previewImage(true)
                        .maxSelectNum(imageAddPickerView.getMaxNum())
                        .selectionMedia(localMedias)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        imageAddPickerView.show();
    }

    private void addSubmitBtn() {
        submitTV = (TextView) LayoutInflater.from(mActivity).inflate(R.layout.menu_text, null);
        submitTV.setText("提交");
        submitTV.setEnabled(false);
        menuView.addView(submitTV);
        menuView.setVisibility(View.VISIBLE);
        menuView.setOnClickListener(v -> {
            if (imageAddPickerView.getSelectList() == null || imageAddPickerView.getSelectList().size() == 0) {
                mPresenter.requestFeedback(ViewUtil.getText(contentET), ViewUtil.getText(contactET));
            } else {
                mPresenter.requestImageFeedback(ViewUtil.getText(contentET), ViewUtil.getText(contactET), imageAddPickerView.getSelectList());
            }
        });
        RxTextView.textChanges(contentET).map(charSequence -> StringUtils.isTrimEmpty(charSequence.toString())).subscribe(aBoolean -> {
            submitTV.setEnabled(!aBoolean);
            menuView.setEnabled(!aBoolean);
        });
    }


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerFeedbackComponent.builder().appComponent(appComponent).feedbackModule(new FeedbackModule(this)).build().inject(this);
    }

    @Override
    protected int layoutId() {
        return R.layout.a_feedback;
    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void onRequesrStart() {
        progressHudUtil.showProgressHud();
    }

    @Override
    public void onRequestFinish() {
        progressHudUtil.dismissProgressHud();
    }

    @Override
    public void feedbackSuccess() {
        ToastUtil.makeToast("谢谢您的反馈,我们会及时跟进");
        finish();
    }


    @OnClick(R.id.copyBtn)
    public void onCopyClick() {
        ToastUtil.makeToast("复制成功");
        ClipboardUtils.copyText("625404666");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                if (data != null) {
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);

                    Observable.fromIterable(selectList).map(localMedia -> localMedia.getPath()).toList().subscribe(list -> imageAddPickerView.replaceAll(list));

                }
            }
        }
    }
}
